﻿
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Labthink.PCSoft.UniversalHost4.UserControls;

namespace Report.Forms
{
    public partial class FrmInputValues2
    {
        public FrmInputValues2()
        {
            InitializeComponent();
            Resources.MergedDictionaries.Add(ReportCommon.GetLan);
        }

        private void FrmInputValues_OnLoaded(object sender, RoutedEventArgs e)
        {
            GridItems.Children.Clear();
            var manager = ReportManager2.Create();
            var c = manager.AddInputs.Count;
            for (int i = 0; i < c; i++)
            {
                GridItems.RowDefinitions.Add(new RowDefinition { Height = new GridLength(50) });
            }

            var index = 0;
            foreach (var sd in manager.ParamSelected.Where(p => !string.IsNullOrEmpty(p.Text)))
            {
                var tb = new UcTextBox { Label = sd.Text, MaxLength = 30, Value = "", Height = 40 };
                Grid.SetRow(tb, index);
                GridItems.RegisterName("txt" + sd.Id, tb);
                GridItems.Children.Add(tb);
                index++;
            }
            //foreach (var sd in manager.ResultSelected.Where(p => !string.IsNullOrEmpty(p.Text)))
            //{
            //    var tb = new TextBlock { Text = sd.Text, Margin = new Thickness(5, 0, 0, 0) , VerticalAlignment=VerticalAlignment.Center};
            //    Grid.SetRow(tb, index);
            //    Grid.SetColumn(tb, 0);
            //    GridItems.Children.Add(tb);
            //    var txt = new TextBox { Margin = new Thickness(5, 0, 5, 0), MaxLength = 30 };
            //    Grid.SetRow(txt, index);
            //    Grid.SetColumn(txt, 1);
            //    GridItems.RegisterName("txt" + sd.Id, txt);
            //    GridItems.Children.Add(txt);
            //    index++;
            //}
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var manager = ReportManager2.Create();
            foreach (var key in manager.AddInputs.Keys.ToList())
            {
                var txt = GridItems.FindName("txt" + key) as UcTextBox;
                if (txt != null)
                {
                    manager.AddInputs[key] = txt.Value;
                    var pi = manager.ParamInfo.SingleOrDefault(p => p.DataNo == key);
                    if (pi != null) pi.Value = txt.Value;
                }
            }
            Close();
        }
    }
}
